﻿using System;
using System.Linq;
using NpCrm.DataAccess.Contracts;
using NpCrm.DataAccess.Core;
using NpCrm.Domain;

namespace NpCrm.DataAccess.Repositories
{
    public class DonationRepository : RepositoryBase<Donation, int>, IDonationRepository
    {
        public IQueryable<MonetaryDonation> GetGreaterThanAmount(decimal floorAmount, bool includeFloor)
        {
            return includeFloor
                       ? ObjectSet.OfType<MonetaryDonation>().Where(md => md.Amount >= floorAmount)
                       : ObjectSet.OfType<MonetaryDonation>().Where(md => md.Amount > floorAmount);
        }

        public IQueryable<MonetaryDonation> GetLessThanAmount(decimal ceilingAmount, bool includeCeiling)
        {
            return includeCeiling
                       ? ObjectSet.OfType<MonetaryDonation>().Where(md => md.Amount <= ceilingAmount)
                       : ObjectSet.OfType<MonetaryDonation>().Where(md => md.Amount < ceilingAmount);
        }

        public IQueryable<MonetaryDonation> GetBetweenAmounts(decimal floorAmount, decimal ceilingAmount, bool includeFloor, bool includeCeiling)
        {
            if(includeFloor)
            {
                return includeCeiling
                           ? ObjectSet.OfType<MonetaryDonation>().Where(
                               md => md.Amount >= floorAmount && md.Amount <= ceilingAmount)
                           : ObjectSet.OfType<MonetaryDonation>().Where(
                               md => md.Amount >= floorAmount && md.Amount < ceilingAmount);
            }
            return includeCeiling
                       ? ObjectSet.OfType<MonetaryDonation>().Where(
                           md => md.Amount > floorAmount && md.Amount <= ceilingAmount)
                       : ObjectSet.OfType<MonetaryDonation>().Where(
                           md => md.Amount > floorAmount && md.Amount < ceilingAmount);
        }

        public IQueryable<ClothingDonation> GetForDonationBin(DonationBin donationBin)
        {
            return ObjectSet.OfType<ClothingDonation>().Where(cd => cd.Bin.DonationBinId == donationBin.DonationBinId);
        }
    }
}
